package cn.dnui_xqh414.dnuifood_16110100414.Fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.android.material.chip.ChipGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder.MyFragmentAdapter;

/**
 * A simple {@link androidx.fragment.app.Fragment} subclass.
 */
public class CollectFragment extends androidx.fragment.app.Fragment {

    private FragmentManager manager;
    private FragmentTransaction fragmentTransaction;

    private List<androidx.fragment.app.Fragment> list;
    private MyFragmentAdapter adapter;

    private RadioGroup radioGroup;

    private Collect_shopFragment collect_shopFragment;
    private Collect_foodFragment collect_foodFragment;
    private RadioButton rb;

    public CollectFragment() {
        // Required empty public constructor
    }

    @Override
    public void onResume() {
        super.onResume();
        rb = getView().findViewById(R.id.rb_shop);
        radioGroup = getView().findViewById(R.id.rg_collect);
        manager = getActivity().getSupportFragmentManager();
        fragmentTransaction = manager.beginTransaction();

        collect_shopFragment = new Collect_shopFragment();
        collect_foodFragment = new Collect_foodFragment();

            switch (radioGroup.getCheckedRadioButtonId()){
                case R.id.rb_shop:
                    fragmentTransaction.replace(R.id.fl, collect_shopFragment);   //用replace而非add来解决重影问题
                    break;
                case R.id.rb_food:
                    fragmentTransaction.replace(R.id.fl, collect_foodFragment);
                    break;
            }
//            fragmentTransaction.replace(R.id.fl,collect_shopFragment);
            fragmentTransaction.commit();
//            rb.setChecked(true);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                fragmentTransaction = manager.beginTransaction();
                switch (checkedId){
                    case R.id.rb_shop:
                        fragmentTransaction.replace(R.id.fl, collect_shopFragment);
                        break;
                    case R.id.rb_food:
                        fragmentTransaction.replace(R.id.fl, collect_foodFragment);
                        break;
                }
                fragmentTransaction.commit();
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        radioGroup = view.findViewById(R.id.rg_collect);
//        manager = getActivity().getSupportFragmentManager();
//        fragmentTransaction = manager.beginTransaction();
//
//        collect_shopFragment = new Collect_shopFragment();
//        collect_foodFragment = new Collect_foodFragment();
//        fragmentTransaction.add(R.id.fl, collect_shopFragment);
//        fragmentTransaction.commit();
//
//        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                fragmentTransaction = manager.beginTransaction();
//                switch (checkedId){
//                    case R.id.rb_shop:
//                        fragmentTransaction.replace(R.id.fl, collect_shopFragment);
//                        break;
//                    case R.id.rb_food:
//                        fragmentTransaction.replace(R.id.fl, collect_foodFragment);
//                        break;
//                }
//                fragmentTransaction.commit();
//            }
//        });

//        list = new ArrayList<>();
//
//        list.add(collect_shopFragment);
//        list.add(collect_foodFragment);
//        String ti[] = new String[0];
//        adapter = new MyFragmentAdapter(getActivity().getSupportFragmentManager(),ti,list);

//        viewPager.setAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_collect, container, false);
    }

}
